﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.Model;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.UI.Models;
using SCMS.CoreBusinessLogic.Request4Advance;
using SCMS.CoreBusinessLogic.Security;
using SCMS.Reports;

namespace SCMS.UI.Areas.Reports.Controllers
{
    [MyException]
    public class R4AController : PortalBaseController
    {

        private IRequest4Advance R4AService;
        public R4AController(IPermissionService permissionService, IRequest4Advance R4AService, IUserContext userContext)
            : base(userContext, permissionService)
        {
            this.R4AService = R4AService;
        }
        //
        // GET: /Reports/R4A/
        public ActionResult pdf(Guid R4Aid)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            AdvanceRequestModel model = this.model(R4Aid);
            using (var db = new SCMSEntities())
            {
                data.Add("{PAYMENT-TYPE}", model.PaymentType);
                data.Add("{REQ_NO.}", model.RefNumber);
                data.Add("{PAYMENT-TO}", model.Payee);
                data.Add("{PAYMENT-TERMS}", model.PaymentTerm);

                data.Add("{DATE}", model.RequestDate.ToString("dd/MM/yyyy"));
                data.Add("{CURRENCY}", model.Currency);
                data.Add("{TOTAL-PAYMENT}", model.TotalAdvance.ToString("#,##0.00"));
                data.Add("{SUBJECT}", model.Subject);

                //data.Add("{PER-DIEM}", model.AdvanceOption == "Per Diem" ? "\" checked=\"checked\"" : "");
                //data.Add("{EXPENSE}", PR.RequestFor == "Expenses" ? "\" checked=\"checked\"" : "");
                //data.Add("{DAILY-ALLOW}", PR.RequestFor == "Daily Allowance" ? "\" checked=\"checked\"" : "");
                //data.Add("{ACCOMODA}", PR.RequestFor == "Accommodation" ? "\" checked=\"checked\"" : "");
                //data.Add("{OTHER}", PR.RequestFor == "Other" ? "\" checked=\"checked\"" : "");

                int count = 1;
                String detailHtml = "";
                foreach (var detail in model.PaymentDetails)
                {
                    count++;
                    detailHtml += "<tr><td>" + detail.Description + "</td><td>";
                    detailHtml += detail.ProjectNo;
                    detailHtml += "</td><td>";
                    detailHtml += detail.BudgetLine;
                    detailHtml += "</td><td align=\"right\">";
                    detailHtml += detail.Amount.ToString("#,###.00");
                    detailHtml += "</td><tr>";
                }
                while (count < 11)
                {
                    count++;
                    detailHtml += "<tr><td>&nbsp;</td><td></td><td></td><td align=\"right\"></td><tr>";
                }
                data.Add("{DETAILS}", detailHtml);

                data.Add("{DESCRIPTION}", model.Description);
                data.Add("{DOC-DISCRIPTION}", model.DocDescription);
                data.Add("{REMARKS}", model.Remarks);

                data.Add("{MB-CURRENCT}", "");// model.MBCurrency);
                data.Add("{MB-TOTAL}", model.MBValue.ToString("#,##.00"));
                data.Add("{VOUCHER-NO}", model.VoucherNo);
                data.Add("{ACCOUNT-NO}", model.AccountNo);

                String authourisation = "";
                authourisation = AuthName(model, authourisation);
                authourisation = AuthTitle(model, authourisation);
                authourisation = AuthDate(model, authourisation);
                authourisation = AuthSignature(model, authourisation);
                data.Add("{AUTHORISATION}", authourisation);

            }
            List<String> options = new List<string>();
            options.Add(" --copies 2 ");
            Byte[] output = WkHtml2Pdf.CreateReport(data, "Request_4_Advance.htm", options);

            return File(output, "application/pdf", "R4A_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }

        private static String AuthSignature(AdvanceRequestModel model, String authourisation)
        {

            authourisation += "<tr><th>Signature:</th><td>";
            if (model.Preparer != null && model.Preparer.Person.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.Preparer.Person.SignatureImage));
            authourisation += "</td><td>";
            if (model.Approver != null && model.Approver.Person.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.Approver.Person.SignatureImage));
            authourisation += "</td><td>";
            if (model.Authorizer != null && model.Authorizer.Person.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.Authorizer.Person.SignatureImage));
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthDate(AdvanceRequestModel model, String authourisation)
        {

            authourisation += "<tr><th>Date:</th><td>";
            authourisation += model.PreparedOn.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td>";
            if (model.Approver != null)
                authourisation += model.ApprovedOn.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td>";
            if (model.Authorizer != null)
                authourisation += model.AuthorizedOn.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthTitle(AdvanceRequestModel model, String authourisation)
        {

            authourisation += "<tr><th>Title:</th><td>";
            if (model.Preparer != null)
                authourisation += model.Preparer.Designation.Name;
            authourisation += "</td><td>";
            if (model.Approver != null)
                authourisation += model.Approver.Designation.Name;
            authourisation += "</td><td>";
            if (model.Authorizer != null)
                authourisation += model.Authorizer.Designation.Name;
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthName(AdvanceRequestModel model, String authourisation)
        {

            authourisation += "<tr><th>Name:</th><td>";
            if (model.Preparer != null)
                authourisation += model.Preparer.Person.FirstName + " " + model.Preparer.Person.OtherNames;
            authourisation += "</td><td>";
            if (model.Approver != null)
                authourisation += model.Approver.Person.FirstName + " " + model.Approver.Person.OtherNames;
            authourisation += "</td><td>";
            if (model.Authorizer != null)
                authourisation += model.Authorizer.Person.FirstName + " " + model.Authorizer.Person.OtherNames;
            authourisation += "</td><tr>";

            return authourisation;
        }


        private AdvanceRequestModel model(Guid r4aid)
        {
            AdvanceRequest entityRFA = R4AService.GetRFAById(r4aid);
            AdvanceRequestModel model = AdvanceRequestExtension.ToModel(entityRFA);
            List<Models.AdvanceRequestBLModel> paymentDetails = new List<AdvanceRequestBLModel>();
            List<Model.AdvanceRequestBudgetLine> rfaBudgetLines = R4AService.GetRFADetails(entityRFA.Id);
            foreach (var rfaBL in rfaBudgetLines)
            {
                var rfaBLModel = AdvanceRequestBLExtension.ToModel(rfaBL);
                paymentDetails.Add(rfaBLModel);
            }
            model.PaymentDetails = paymentDetails;
            return model;
        }
    }
}
